package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMTxnOrderMapper;
import cc.rengu.igas.momp.common.entity.TMTxnOrder;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单流水表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/17 10:09
 */

public class TMTxnOrderMapperImpl implements TMTxnOrderMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 新增单条订单流水数据库操作接口实现
     *
     * @param tmTxnOrder 订单流水表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/17 10:10
     */
    @Override
    public int insertSingleRecordIntoTMTxnOrder(TMTxnOrder tmTxnOrder) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleRecordIntoTMTxnOrder");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrder, TMTxnOrder.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>流水号<{}>新增订单数据失败! RETURN_CODE=<{}>", tmTxnOrder.getInstId(), tmTxnOrder.getOrderId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 取交易流水信息数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMTxnOrder 订单流水表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 09:23
     */
    @Override
    public TMTxnOrder selectTransactionOrderInfo(String instId, String transDate, String traceNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOriginalTransactionOrderInfo");

        TMTxnOrder tmTxnOrder = new TMTxnOrder();
        tmTxnOrder.setInstId(instId);
        tmTxnOrder.setTxnDate(transDate);
        tmTxnOrder.setFrontSeqNum(traceNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmTxnOrder, tmTxnOrder);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询法人机构号<{}>在<{}>的流水号<{}>的订单数据失败! RETURN_CODE=<{}>", instId, transDate, traceNo, iReturnCode);
            return null;
        }

        return tmTxnOrder;
    }

    /**
     * 合并支付冲正服务更新原交易流水业务状态数据库操作实现
     *
     * @param tmTxnOrder 订单流水表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 16:00
     */
    @Override
    public int updateOriginalTransactionOrderBizStatus(TMTxnOrder tmTxnOrder) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateOriginalTransactionOrderBizStatus");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrder, TMTxnOrder.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>在<{}>的流水号<{}>业务状态失败! RETURN_CODE=<{}>", tmTxnOrder.getInstId(), tmTxnOrder.getTxnDate(), tmTxnOrder.getFrontSeqNum(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 手续费优惠冲正服务获取原交易流水信息数据库操作接口定义
     *
     * @param instId      法人机构号
     * @param transDate   交易日期
     * @param origTraceNo 原交易流水号
     * @return cc.rengu.igas.momp.common.entity.TMTxnOrder 订单流水表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/8
     */
    @Override
    public TMTxnOrder selectFeeDiscountOriginalTransactionOrderInfo(String instId, String transDate, String origTraceNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectFeeDiscountOriginalTransactionOrderInfo");

        TMTxnOrder tmTxnOrder = new TMTxnOrder();
        tmTxnOrder.setInstId(instId);
        tmTxnOrder.setTxnDate(transDate);
        tmTxnOrder.setFrontSeqNum(origTraceNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmTxnOrder, tmTxnOrder);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询法人机构号<{}>在<{}>的流水号<{}>订单数据失败! RETURN_CODE=<{}>", instId, transDate, origTraceNo, iReturnCode);
            return null;
        }
        return tmTxnOrder;
    }

    /**
     * 手续费优惠冲正服务更新原交易流水业务状态数据库操作定义
     *
     * @param tmTxnOrder 订单流水表
     * @param bizStatus  业务状态
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/8
     */
    @Override
    public int updateFeeDiscountOriginalTransactionOrderBizStatus(TMTxnOrder tmTxnOrder, String bizStatus) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompUpdateFeeDiscountOriginalTransactionOrderBizStatus");

        String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
        tmTxnOrder.setBizStatus(bizStatus);
        tmTxnOrder.setUpdateTime(timeStamp);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrder, TMTxnOrder.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>在<{}>的流水号<{}>业务状态失败! RETURN_CODE=<{}>", tmTxnOrder.getInstId(), tmTxnOrder.getTxnDate(),
                    tmTxnOrder.getOrigOrderId(), iReturnCode);
            return iReturnCode;
        }
        return Database.DBS_SUCCESS;
    }
}
